package com.cloudsigns.factorymanager2.engineering.customview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.DialogItemBean;

import java.util.List;

/**
 * Created by tony on 2018/4/12.
 */

public class BottomListDialog extends Dialog {
    private List<DialogItemBean> items;
    private Context mContext;
    private LinearLayout dialogView;
    private OnItemDialogClick itemClick;
    public static boolean canLongClick = false;;
    public OnItemDialogClick getItemClick() {
        return itemClick;
    }

    public void setItemClick(OnItemDialogClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface OnItemDialogClick {
        void onItemDialogClick(DialogItemBean item);
    }


    public BottomListDialog(Context context, List<DialogItemBean> items) {
        super(context , R.style.ItemDialog);
        this.items = items;
        this.mContext = context;
        init();
    }
    @Override
    public void show() {
        canLongClick =true;
        super.show();
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    private void init() {
        dialogView = (LinearLayout) LayoutInflater.from(mContext)
                .inflate(R.layout.dialog_list, null);
        TextView textView;
        ImageView imageView;
        View mainRow;
        for (final DialogItemBean item : items) {
            final LinearLayout itemView = (LinearLayout) LayoutInflater.from(mContext).inflate(item.getmViewId(), null);
            textView = (TextView) itemView.findViewById(R.id.popup_text);
            textView.setText(item.getmTextId());
            textView.setClickable(true);

            mainRow = itemView.findViewById(R.id.dialog_row);


            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClick != null) {
                        itemClick.onItemDialogClick(item);
                        dismiss();
                    }
                }
            });
            if (mainRow != null) {
                mainRow.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (itemClick != null) {
                            itemClick.onItemDialogClick(item);
                            dismiss();
                        }
                    }
                });
            }
            dialogView.addView(itemView);
        }

        WindowManager.LayoutParams localLayoutParams = getWindow()
                .getAttributes();
        localLayoutParams.x = 0;
        localLayoutParams.y = -1000;
        localLayoutParams.gravity = Gravity.BOTTOM;
        dialogView.setMinimumWidth(10000);

        onWindowAttributesChanged(localLayoutParams);
        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }

    @Override
    public void dismiss() {
        canLongClick =false;
        super.dismiss();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(dialogView);
    }

}
